//
//  LGShotrCell.m
//  lgdw
//
//  Created by mac on 16/1/16.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "LGShotrCell.h"
#import "LGShotr.h"

// 间距
#define Margen 10

@interface LGShotrCell ()
/** 用户头像 */
@property (nonatomic, strong) UIImageView *avatarView;
/** 用户姓名 */
@property (nonatomic, strong) UILabel *namaLabel;
/** 关注状态 */
@property (nonatomic , strong) UIButton *followBtn;
/** 大图 */
@property (nonatomic, strong) UIImageView *iconView;
/** 标题信息 */
@property (nonatomic, strong) UILabel *titleLabel;
/** 点赞 */
@property (nonatomic, strong) UIButton *like_numBtn;
/** 线 */
@property (nonatomic, strong) UILabel *lineView;
/** 评论 */
@property (nonatomic, strong) UIButton *comment_num;
/** 间距 */
@property (nonatomic, strong) UIView *margnView;
@end


@implementation LGShotrCell

#pragma -mark /***************** 设置传过来的数据 *****************/
- (void)setShotrModel:(LGShotr *)shotrModel {
    // 防止重用
    self.avatarView.image = nil;
    self.namaLabel.text = nil;
    [self.followBtn setTitle:nil forState:UIControlStateNormal];
    self.iconView.image = nil;
    self.titleLabel.text = nil;
    [self.like_numBtn setTitle:nil forState:UIControlStateNormal];
    [self.comment_num setTitle:nil forState:UIControlStateNormal];
    
    _shotrModel = shotrModel;
    // 头像
    [self.avatarView sd_setImageWithURL:[NSURL URLWithString:shotrModel.avatar] placeholderImage:[UIImage imageNamed:@"icon_80"]];
    // 名字
    self.namaLabel.text = shotrModel.name;
    // 关注
    if (shotrModel.follow_status == 0) {
        self.followBtn.selected = NO;
        [self.followBtn setTitle:@"关注" forState:UIControlStateNormal];
        // 线的颜色
        self.followBtn.layer.borderColor = [[UIColor alloc] initWithRed:63 / 255.0 green:156 / 255.0 blue:157 / 255.0 alpha:1.0].CGColor;
        
    } else {
        self.followBtn.selected = YES;
        [self.followBtn setTitle:@"已关注" forState:UIControlStateSelected];
        // 线的颜色
        self.followBtn.layer.borderColor = [UIColor grayColor].CGColor;
    }
    
    // 点击关注
    [self.followBtn addTarget:self action:@selector(followBtnClick) forControlEvents:UIControlEventTouchUpInside];
    // 大图
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:shotrModel.pic] placeholderImage:[UIImage imageNamed:@"placeholder_image"]];
    // 标题
    self.titleLabel.text = shotrModel.title;
    // 点赞
    [self.like_numBtn setTitle:[NSString stringWithFormat:@"  %@",shotrModel.like_num] forState:UIControlStateNormal];
    [self.like_numBtn addTarget:self action:@selector(like_numBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    // 评论
    [self.comment_num setTitle:[NSString stringWithFormat:@"  %@",shotrModel.comment_num] forState:UIControlStateNormal];
    [self.comment_num addTarget:self action:@selector(comment_numClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
}
#pragma -mark /***************** 点击事件 *****************/
- (void)followBtnClick {
    self.followBtn.selected = !self.followBtn.selected;
    if (self.followBtn.selected == YES) {
        self.followBtn.layer.borderColor = [UIColor grayColor].CGColor;
    } else {
        // 线的颜色
        self.followBtn.layer.borderColor = [[UIColor alloc] initWithRed:63 / 255.0 green:156 / 255.0 blue:157 / 255.0 alpha:1.0].CGColor;
    }
}
/** 点击赞  */
- (void)like_numBtnClick:(UIButton *)btn {
    btn.selected = !btn.selected;
    if (!btn.selected) {
        [self.like_numBtn setTitle:[NSString stringWithFormat:@"  %@",self.shotrModel.like_num] forState:UIControlStateNormal];
        return;
    }
    [btn setTitle:[NSString stringWithFormat:@"  %d",[self.shotrModel.like_num intValue] + 1] forState:UIControlStateNormal];
    
}
/** 点击评论  */
- (void)comment_numClick:(UIButton *)btn {
    
    NSLog(@"点击晒物页面的评论");
    
}

#pragma -mark /***************** 重写init方法 *****************/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}


// 设置界面
- (void)setupUI {
    self.backgroundColor = [[UIColor alloc] initWithWhite:1.0 alpha:1.0];
    
    // 添加控件
    [self addSubview:self.avatarView];
    [self addSubview:self.namaLabel];
    [self addSubview:self.followBtn];
    [self addSubview:self.iconView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.comment_num];
    [self addSubview:self.lineView];
    [self addSubview:self.like_numBtn];
    [self addSubview:self.margnView];
    // 设置布局
    // 头像
    [self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(Margen);
        make.width.mas_equalTo(30);
        make.top.mas_equalTo(4);
        make.height.mas_equalTo(30);
        
    }];
    // 名字
    [self.namaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.avatarView.mas_centerY);
        make.left.mas_equalTo(self.avatarView.mas_right).offset(Margen);
        
    }];
    // 关注
    
    [self.followBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(- 2 * Margen);
        make.centerY.mas_equalTo(self.avatarView.mas_centerY);
    }];
    // 大图
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(Margen);
        make.right.mas_equalTo(self.mas_right).offset(-Margen);
        make.top.mas_equalTo(self.avatarView.mas_bottom).offset(Margen);
        CGFloat heig = self.frame.size.width * 0.7;
        make.height.mas_equalTo(heig);
        
    }];
    // 标题
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconView.mas_bottom).offset(Margen);
        make.left.mas_equalTo(self.mas_left).offset(Margen);
        make.right.mas_equalTo(self.mas_right).offset(-Margen);
        
    }];
    // 评论
    [self.comment_num mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-4 * Margen);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(Margen / 2);
        
    }];
    // 线
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(10);
        make.centerY.mas_equalTo(self.comment_num.mas_centerY);
        make.right.mas_equalTo(self.comment_num.mas_left).offset(-2 * Margen);
        make.width.mas_equalTo(1);
        
    }];
    // 点赞
    [self.like_numBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.lineView.mas_left).offset(-2 * Margen);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(Margen / 2);
    }];
    // 间隔
    [self.margnView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.like_numBtn.mas_bottom).offset(Margen / 2);
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        make.bottom.mas_equalTo(self.mas_bottom);
        make.height.mas_equalTo(Margen);
    }];
    
    
}

#pragma -mark /***************** 懒加载 *****************/
// 头像
- (UIImageView *)avatarView {
    if (_avatarView == nil) {
        _avatarView = [[UIImageView alloc] initAndClipsToBounds];
        // 圆形图片
        _avatarView.layer.cornerRadius = 15;
        _avatarView.layer.borderWidth = 1;
        _avatarView.layer.borderColor = [[UIColor alloc] initWithWhite:0.5 alpha:0.5].CGColor;
        _avatarView.layer.masksToBounds = YES;
        [_avatarView setImage:[UIImage imageNamed:@"icon_80"]];
        
    }
    return _avatarView;
}
// name
- (UILabel *)namaLabel {
    if (_namaLabel == nil) {
        _namaLabel = [[UILabel alloc] initWithTitle:@"aaa" FontSize:12 TextColor:nil];
        
    }
    return _namaLabel;
}
// 关注状态
- (UIButton *)followBtn {
    if (_followBtn == nil) {
        _followBtn = [[UIButton alloc] init];
        // Normal
        [_followBtn setTitle:@"关注" forState:UIControlStateNormal];
        // 颜色
        [_followBtn setTitleColor:[[UIColor alloc] initWithRed:63 / 255.0 green:156 / 255.0 blue:157 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        // select
        [_followBtn setTitle:@"已关注" forState:UIControlStateSelected];
        // 颜色
        [_followBtn setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
        [_followBtn sizeToFit];
        // 大小
        _followBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_followBtn setContentEdgeInsets:UIEdgeInsetsMake(4, 10, 4, 10)];
        [_followBtn sizeToFit];
        // 边框
        _followBtn.layer.borderWidth = 0.5;
        // 圆角
        _followBtn.layer.cornerRadius = 4.5;
        
    }
    return _followBtn;
}

// 大图
- (UIImageView *)iconView {
    if (_iconView == nil) {
        _iconView = [[UIImageView alloc] initAndClipsToBounds];
        _iconView.image = [UIImage imageNamed:@"placeholder_image"];
    }
    return _iconView;
}
// 标题
- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] initWithTitle:@"阿里附加辣椒放辣椒放多了就爱疯了搭建费建安路附近阿里放假阿里放假放假阿里解放垃圾费的啦" FontSize:14 TextColor:nil];
        _titleLabel.numberOfLines = 2;
        
    }
    return _titleLabel;
}
// 点赞
- (UIButton *)like_numBtn {
    if (_like_numBtn == nil) {
        _like_numBtn = [[UIButton alloc] initWithTitle:@"  -1" ImageName:@"detail_like"];
        
    }
    
    return _like_numBtn;
}
// 线
- (UILabel *)lineView {
    if (_lineView == nil) {
        _lineView = [[UILabel alloc] init];
        _lineView.backgroundColor = [[UIColor alloc] initWithWhite:0.7 alpha:0.5];
    }
    return _lineView;
}
// 评论
- (UIButton *)comment_num {
    if (_comment_num == nil) {
        _comment_num = [[UIButton alloc] initWithTitle:@"  -0" ImageName:nil];
        
        [_comment_num setImage:[UIImage imageNamed:@"shai_comment"] forState:UIControlStateNormal];
    }
    return _comment_num;
}
// 间隔
- (UIView *)margnView {
    
    if (_margnView == nil) {
        _margnView = [[UIView alloc] init];
        _margnView.backgroundColor = [[UIColor alloc] initWithWhite:0.95 alpha:1.0];
    }
    return _margnView;
}

@end
